<template>
  <div>
    <!-- 三级分类 -->
    <Category :scene="scene"></Category>
    <el-card style="margin: 10px 0px">
      <!-- 展示已有的SPU数据 -->
      <div v-show="scene == 0">
        <el-button
          type="primary"
          size="default"
          icon="Plus"
          :disabled="categoryStore.c3Id ? false : true"
          @click="addSpu"
        >
          添加SPU
        </el-button>
        <el-table border style="margin: 10px" :data="records">
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="80px"
          ></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column
            label="SPU描述"
            prop="description"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="SPU操作">
            <!-- 使用模板插槽 row即相当于当前行的对象 -->
            <template #="{ row, $index }">
              <el-button
                type="primary"
                size="small"
                icon="Plus"
                title="添加SKU"
                @click="addSku(categoryStore.c1Id, categoryStore.c2Id, row)"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                icon="Edit"
                title="修改SPU"
                @click="editSpu(row)"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                icon="View"
                title="查看SKU列表"
                @click="showSkuTabel(row)"
              ></el-button>
              <el-popconfirm
                :title="`确定删除${row.spuName}`"
                width="200px"
                @confirm="deleteSpu(row.id)"
              >
                <template #reference>
                  <el-button
                    type="primary"
                    size="small"
                    icon="Delete"
                    title="删除当前SPU"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 添加|修改已有的SPU -->
      <SpuForm
        ref="spu"
        v-show="scene == 1"
        @changeScene="changeScene"
      ></SpuForm>

      <!-- 添加已有SPU的SKU属性 -->
      <SkuForm
        ref="sku"
        v-show="scene == 2"
        @changeScene="changeScene"
      ></SkuForm>
    </el-card>
    <!-- 分页器 -->
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      @current-change="getHasSpu"
      @size-change="changeSize"
      :page-sizes="[3, 5, 7, 9]"
      :background="true"
      layout=" prev, pager, next, jumper,->, sizes,total"
      :total="total"
    />

    <!-- SKU详情列表对话框 -->
    <el-dialog v-model="showSkuInfo" title="Sku列表" width="800">
      <el-table :data="SkuInfoList">
        <el-table-column label="名字" prop="skuName" width="200" />
        <el-table-column label="价格" prop="price" width="200" />
        <el-table-column label="重量" prop="weight" width="200" />
        <el-table-column label="图片" width="200">
          <template #="{ row, index }">
            <img :src="row.skuDefaultImg" style="height: 60px; width: auto" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { onBeforeUnmount, ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
// 引入分类管理小仓库
import useCategoryStore from '../../../store/modules/category'
import {
  reqHasSpu,
  reqSelectSkuInfoById,
  reqDeleteSpu,
} from '../../../api/product/spu/index'
import type {
  HasSpuResponseData,
  Records,
  SkuData,
  SpuData,
} from '../../../api/product/spu/type'
import SkuForm from './skuForm.vue'
import SpuForm from './spuForm.vue'
//场景的数据
// 场景0:展示已有SPU数据 场景1:新增|修改SPU 场景2:新增SKU
let scene = ref<number>(0)
//分页器默认页码
let pageNo = ref<number>(1)
//每一页展示几条数据
let pageSize = ref<number>(3)

//存储已有SPU总个数
let total = ref<number>(0)

let categoryStore = useCategoryStore()

let records = ref<Records>([])

//获取子组件实例SpuForm
let spu = ref<any>()

// 定义sku子组件响应式对象
let sku = ref<any>()

// 控制SKU详情列表的响应式变量
let showSkuInfo = ref(false)

// 定义展示SPU下SKU数据列表的对象
let SkuInfoList = ref<SkuData>()

// 监听catogoryId3的变化
watch(
  () => categoryStore.c3Id,
  () => {
    // 当三级分类发生变化时,发送请求获取当前三界分类下的SPU数据
    getHasSpu()
  },
)

// 获取三级分类下的分页数据
const getHasSpu = async (pager = 1) => {
  // 当pager不带参数是 默认拉取第一页的数据
  pageNo.value = pager
  let result: HasSpuResponseData = await reqHasSpu(
    pageNo.value,
    pageSize.value,
    categoryStore.c3Id,
  )
  //console.log(result);
  if ((result.code = 200)) {
    records.value = result.data.records
    total.value = result.data.total
  }
}

//分页器下拉菜单发生变化的时候触发
const changeSize = (obj: any) => {
  getHasSpu()
}

// 添加SPU的方法
const addSpu = () => {
  scene.value = 1
  //调用子组件的方法 传送当前三级分类ID
  spu.value.initAddSpu(categoryStore.c3Id)
}

// 子组件绑定的自定义事件
const changeScene = (obj: any) => {
  scene.value = obj.flag
  // 子组件Spuform点击取消变为场景0:展示已有的SPU数据
  if (obj.params == 'update') {
    // 修改完SPU数据,重新获取当前三级分类下的SPU数据
    getHasSpu(pageNo.value)
  } else {
    // 添加留在第一页
    getHasSpu()
  }
}

// 修改SPU的方法
const editSpu = (row: SpuData) => {
  scene.value = 1
  //调用子组件的方法,传递当前行的SPU对象
  spu.value.initHasSpuData(row)
}

// 添加SKU的方法
const addSku = (c1Id, c2Id, row: any) => {
  scene.value = 2
  console.log(row)
  // 调用子组件的方法
  sku.value.initSkuData(c1Id, c2Id, row)
}

// 控制展示SKU详情列表的方法
const showSkuTabel = async (row: any) => {
  showSkuInfo.value = true
  let result = await reqSelectSkuInfoById(row.id)

  // 存储数据
  SkuInfoList.value = result.data

  //console.log('获取到的SKU信息有', JSON.stringify(result))
}

// 删除某个spu
const deleteSpu = async (id: number) => {
  let result = await reqDeleteSpu(id)
  if ((result.code = 200)) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    // 删除成功之后刷新当前页面
    getHasSpu(records.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}

// 路由组件销毁前，清空仓库相关的分类数据
onBeforeUnmount(() => {
  categoryStore.$reset()
})
</script>

<style lang="scss" scoped></style>
